<?php
/***********************************************
	This class would encode PHP data into JSON
	data.
************************************************/
class JSONEncoder {
	private $isList = NULL;
	private $jsonReplaces = NULL;
	private $encodedJSONData = NULL;
	
	/************************************************************/
	
	function __construct() {
		$this->isList = TRUE;
		$this->encodedJSONData = array();
	}
	
	/************************************************************/
	
	function encodeJSONData($searchResults) {
		if (!function_exists('json_encode')) {
    		function json_encode($searchResults) {
				if (is_scalar($searchResults)) {
            		if (is_float($searchResults)) {
    	            	return floatval(str_replace(',', '.', strval($searchResults)));
            		}
            	
					if (is_string($searchResults)) {
	                	$this->jsonReplaces = array(
							array('\\', '/', "\n", "\t", "\r", "\b", "\f", '"'),
							array('\\\\', '\\/', '\\n', '\\t', '\\r', '\\b', '\\f', '\"')
						);
						
    	            	return '"'.str_replace($this->jsonReplaces[0], $this->jsonReplaces[1], $searchResults).'"';
        	    	}
            	
					else return $searchResults;
        		}
        
				for ($i = 0, reset($searchResults); TRUE; $i++) {
            		if (key($searchResults) !== $i) {
	                	$this->isList = FALSE;
    	            	break;
            		}
        		}
        
				if ($this->isList) {
	            	foreach ($searchResults as $value) {
						$this->encodedJSONData[] = json_encode($value);
 		   	        	return '['.join(',', $this->encodedJSONData).']';
        			}
				} else {
	            	foreach ($searchResults as $key => $value) {
						$this->encodedJSONData[] = json_encode($key).':'.json_encode($value);
    	        		return '{'.join(',', $this->encodedJSONData).'}';
        			}
    			}
			}
		}
	
		$this->encodedJSONData = json_encode($searchResults);
		return $this->encodedJSONData;
	}
}
?>